package org.myOrg.spetrum.GUI;

import java.util.Iterator;
import java.util.Map.Entry;

import org.myOrg.spetrum.Driver.SpectrumUnion;
import org.myOrg.spetrum.io.InputManager;
import org.myOrg.spetrum.spectrumStruct.Spectrum;
import org.myOrg.spetrum.spectrumStruct.SpectrumFactory;
import org.myOrg.spetrum.testObject.TestCase;
import org.myOrg.spetrum.testObject.TestSuite;
import org.myOrg.spetrum.util.Message;
import org.myOrg.spetrum.util.MessageMangement;
import org.myOrg.spetrum.util.MessageMangementImplement;
import org.myOrg.spetrum.util.OverallFactory;
import org.myOrg.spetrum.util.Reciver;

public class RunnedTestsPanel extends ShowPanel implements Reciver {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public RunnedTestsPanel() {
		this.register(MessageMangementImplement.RUNOVER, OverallFactory
				.getMessageManger());
		this.register(MessageMangementImplement.READOVER, OverallFactory
				.getMessageManger());
	}

	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		if (msg.getType() == MessageMangementImplement.RUNOVER) {
			this.clear();
			if(msg.getInfo() instanceof Spectrum){
				Spectrum spectrum = (Spectrum)msg.getInfo() ;
				showTestSuiteNum(spectrum);
				showSpectrumRunnedTestSuites(spectrum);
			}else{
				SpectrumUnion specrums = (SpectrumUnion)msg.getInfo();
				Iterator<Entry<Integer,Spectrum>> itr = specrums.itertor();
				while(itr.hasNext()){
					Entry<Integer, Spectrum> entry = itr.next();
					showAlogrithm(entry.getKey().intValue());
					showTestSuiteNum(entry.getValue());		
				}
				itr = specrums.itertor();
				while(itr.hasNext()){
					Entry<Integer, Spectrum> entry = itr.next();
					showAlogrithm(entry.getKey().intValue());
					showSpectrumRunnedTestSuites(entry.getValue());
				}
			}

			
	
		} else if (msg.getType() == MessageMangementImplement.READOVER) {
			InputManager input = (InputManager) msg.getInfo();
			if (input.isFormatPass()) {
				this.clear();
			}
		}
	}
	
	private void showTestSuiteNum(Spectrum spectrum){
		TestSuite suite = spectrum.getGenerateTestCases();
		String result = "all:" + suite.getTestCaseNum() + "  pass:"
		+ suite.getInfoTestCases(TestCase.PASSED).getTestCaseNum()
		+ "  fail:"
		+ suite.getInfoTestCases(TestCase.FAILED).getTestCaseNum();
		this.insertOneLine(result,
				StyleSet.getStyleConsleRight());
	}
	private void showAlogrithm(int type){
		this.insertOneLine(SpectrumFactory.getName(type),
				StyleSet.getStyleTitle());
	}
	private void showSpectrumRunnedTestSuites(Spectrum spectrum){
		TestSuite suite = spectrum.getGenerateTestCases();

		this.insertOneLine("the tests generate during the program:",
				StyleSet.getStyleConsleRight());

		for (int i = 0; i < suite.getTestCaseNum(); i++) {
			if (suite.getAt(i).testDescription() == TestCase.PASSED)
				this.insertOneLine(suite.getAt(i).getStringOfTest(),
						StyleSet.getStylePassed());
			else
				this.insertOneLine(suite.getAt(i).getStringOfTest(),
						StyleSet.getStyleFailed());
		}
	}

	@Override
	public void register(int messageType, MessageMangement messageManger) {
		// TODO Auto-generated method stub
		messageManger.register(this, messageType);
	}

}
